/**
 * This file is part of JCueFile Creator.
 * 
 * JCueFile Creator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * (at your option) any later version.
 * 
 * JCueFile Creator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 *  along with JCueFile Creator.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jcuefile.GUI;

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import org.jcuefile.config.Constants;
import org.jcuefile.cuefiles.CueFileManager;

final public class GUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2308956322173697298L;
	final private Vector<String> columnNames = new Vector<String>();
	final private static Vector<Object> rowData = new Vector<Object>();
	
	private static JPanel content;
	private static JTextField artistField;
	private static JTextField titleField;
	private static JTable table;
	private static JTextField fileField;
	private static JPanel CFIPanel;
	private JPanel panel_3;
	private JLabel lblAuthor;
	private JLabel lblTitle;
	private JLabel lblFile;
	private JPanel panel_5;
	private JButton openButton;
	private JPanel SIPanel;
	private JPanel panel_4;
	private JButton addRowButton;
	private JButton deleteRowButton;
	private JScrollPane scrollPane;
	private JPanel ButtonsPanel;
	private JButton saveButton;
	private JButton loadButton;
	private JMenuBar menu;
	private JMenu fileMenu;
	private JMenuItem fmNew;
	private JMenuItem fmOpen;
	private JMenuItem fmSave;
	private JMenuItem fmExit;
	private JMenu helpMenu;
	private JMenuItem hmVersion;
	private JMenuItem hmAbout;

	/**
	 * Create the frame.
	 */
	public GUI() {
		this.columnNames.add("Nr");
		this.columnNames.add("Artist");
		this.columnNames.add("Title");
		this.columnNames.add("Index");
		Vector<Object> vec = new Vector<Object>();
		vec.add("01");
		vec.add("");
		vec.add("");
		vec.add("");
		rowData.add(vec);
		showGUI();
	}
	
	final private void showGUI() {
		this.setTitle(Constants.getProjectName());
		this.setMinimumSize(new Dimension(600, 600));
		this.setSize(new Dimension(600, 600));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			JOptionPane.showMessageDialog(this, "Error loading application! code: 1", "Error!", JOptionPane.ERROR_MESSAGE);
		} catch (InstantiationException e) {
			JOptionPane.showMessageDialog(this, "Error loading application! code: 2", "Error!", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalAccessException e) {
			JOptionPane.showMessageDialog(this, "Error loading application! code: 3", "Error!", JOptionPane.ERROR_MESSAGE);
		} catch (UnsupportedLookAndFeelException e) {
			try {
				UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			} catch (InstantiationException e1) {
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				e1.printStackTrace();
			} catch (UnsupportedLookAndFeelException e1) {
				e1.printStackTrace();
			}
		}

		this.setBounds(100, 100, 587, 427);
		
		this.menu = new JMenuBar();
		this.setJMenuBar(this.menu);
		
		this.fileMenu = new JMenu("File");
		this.menu.add(this.fileMenu);
		
		this.fmNew = new JMenuItem("New Cue File");
		this.fmNew.addActionListener(new ActionListener() {
			final public void actionPerformed(ActionEvent e) {
				CueFileManager.newCueFile();
			}
		});
		this.fileMenu.add(this.fmNew);
		
		this.fmOpen = new JMenuItem("Load Cue File");
		this.fmOpen.addActionListener(new ActionListener() {
			final public void actionPerformed(ActionEvent e) {
				CueFileManager.loadCueFile();
			}
		});
		this.fileMenu.add(this.fmOpen);
		
		this.fmSave = new JMenuItem("Save Cue File");
		this.fmSave.addActionListener(new ActionListener() {
			final public void actionPerformed(ActionEvent e) {
				CueFileManager.saveCueFile();
			}
		});
		this.fileMenu.add(this.fmSave);
		
		this.fileMenu.addSeparator();
		
		this.fmExit = new JMenuItem("Exit");
		this.fmExit.addActionListener(new ActionListener() {
			final public void actionPerformed(ActionEvent e) {
				int confirmExit = JOptionPane.showConfirmDialog(content, "Are you sure you want to Quit?", "Quit Cue File Generator!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				if (confirmExit == JOptionPane.YES_OPTION) {
					System.exit(0);
				}
			}
		});
		this.fileMenu.add(this.fmExit);
		
		this.helpMenu = new JMenu("Help");
		this.menu.add(this.helpMenu);
		
		this.hmVersion = new JMenuItem("Version");
		this.hmVersion.addActionListener(new ActionListener() {
			final public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(content, new String[] {
						Constants.getProjectName(), 
						"Version: " + Constants.getProjectVersion()
				}, "Version", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		this.helpMenu.add(this.hmVersion);
		
		this.helpMenu.addSeparator();
		
		this.hmAbout = new JMenuItem("About");
		this.hmAbout.addActionListener(new ActionListener() {
			final public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(content, new String[]{
						"An opensource Java CueFile Creator!",
						"Visit http://code.google.com/p/jcuefile-creator/ for more information."
				}, "About", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		this.helpMenu.add(this.hmAbout);
		
		content = new JPanel();
		content.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.setContentPane(content);
		GridBagLayout gbl_content = new GridBagLayout();
		gbl_content.columnWidths = new int[]{380, 0};
		gbl_content.rowHeights = new int[]{39, 0, 0, 0};
		gbl_content.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_content.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		content.setLayout(gbl_content);
		
		CFIPanel = new JPanel();
		CFIPanel.setBorder(new TitledBorder(null, "Cue File Information", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_CFIPanel = new GridBagConstraints();
		gbc_CFIPanel.fill = GridBagConstraints.HORIZONTAL;
		gbc_CFIPanel.insets = new Insets(0, 0, 5, 0);
		gbc_CFIPanel.anchor = GridBagConstraints.NORTH;
		gbc_CFIPanel.gridx = 0;
		gbc_CFIPanel.gridy = 0;
		content.add(CFIPanel, gbc_CFIPanel);
		GridBagLayout gbl_CFIPanel = new GridBagLayout();
		gbl_CFIPanel.columnWidths = new int[]{118, 0};
		gbl_CFIPanel.rowHeights = new int[]{22, 0};
		gbl_CFIPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_CFIPanel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		CFIPanel.setLayout(gbl_CFIPanel);
		
		this.panel_3 = new JPanel();
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.fill = GridBagConstraints.BOTH;
		gbc_panel_3.gridx = 0;
		gbc_panel_3.gridy = 0;
		CFIPanel.add(this.panel_3, gbc_panel_3);
		GridBagLayout gbl_panel_3 = new GridBagLayout();
		gbl_panel_3.columnWidths = new int[]{118, 0, 0};
		gbl_panel_3.rowHeights = new int[]{22, 0, 0, 0};
		gbl_panel_3.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gbl_panel_3.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		this.panel_3.setLayout(gbl_panel_3);
		
		this.lblAuthor = new JLabel("Artist");
		GridBagConstraints gbc_lblAuthor = new GridBagConstraints();
		gbc_lblAuthor.anchor = GridBagConstraints.EAST;
		gbc_lblAuthor.insets = new Insets(5, 0, 5, 5);
		gbc_lblAuthor.gridx = 0;
		gbc_lblAuthor.gridy = 0;
		this.panel_3.add(this.lblAuthor, gbc_lblAuthor);
		this.lblAuthor.setHorizontalTextPosition(SwingConstants.LEFT);
		this.lblAuthor.setHorizontalAlignment(SwingConstants.LEFT);
		
		artistField = new JTextField();
		artistField.setMargin(new Insets(5, 5, 5, 5));
		artistField.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_artistField = new GridBagConstraints();
		gbc_artistField.fill = GridBagConstraints.HORIZONTAL;
		gbc_artistField.insets = new Insets(5, 5, 0, 5);
		gbc_artistField.gridx = 1;
		gbc_artistField.gridy = 0;
		this.panel_3.add(artistField, gbc_artistField);
		artistField.setColumns(10);
		
		this.lblTitle = new JLabel("Title");
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.anchor = GridBagConstraints.EAST;
		gbc_lblTitle.insets = new Insets(5, 0, 5, 5);
		gbc_lblTitle.gridx = 0;
		gbc_lblTitle.gridy = 1;
		this.panel_3.add(this.lblTitle, gbc_lblTitle);
		
		titleField = new JTextField();
		titleField.setMargin(new Insets(5, 5, 5, 5));
		titleField.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_titleField = new GridBagConstraints();
		gbc_titleField.fill = GridBagConstraints.HORIZONTAL;
		gbc_titleField.insets = new Insets(5, 5, 0, 5);
		gbc_titleField.gridx = 1;
		gbc_titleField.gridy = 1;
		this.panel_3.add(titleField, gbc_titleField);
		titleField.setColumns(10);
		
		this.lblFile = new JLabel("File");
		GridBagConstraints gbc_lblFile = new GridBagConstraints();
		gbc_lblFile.anchor = GridBagConstraints.EAST;
		gbc_lblFile.insets = new Insets(0, 0, 0, 5);
		gbc_lblFile.gridx = 0;
		gbc_lblFile.gridy = 2;
		this.panel_3.add(this.lblFile, gbc_lblFile);
		
		this.panel_5 = new JPanel();
		GridBagConstraints gbc_panel_5 = new GridBagConstraints();
		gbc_panel_5.fill = GridBagConstraints.BOTH;
		gbc_panel_5.gridx = 1;
		gbc_panel_5.gridy = 2;
		this.panel_3.add(this.panel_5, gbc_panel_5);
		GridBagLayout gbl_panel_5 = new GridBagLayout();
		gbl_panel_5.columnWidths = new int[]{0, 0, 0};
		gbl_panel_5.rowHeights = new int[]{0, 0};
		gbl_panel_5.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		gbl_panel_5.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		this.panel_5.setLayout(gbl_panel_5);
		
		fileField = new JTextField();
		fileField.setMargin(new Insets(5, 5, 5, 0));
		GridBagConstraints gbc_fileField = new GridBagConstraints();
		gbc_fileField.fill = GridBagConstraints.HORIZONTAL;
		gbc_fileField.insets = new Insets(5, 5, 5, 0);
		gbc_fileField.gridx = 0;
		gbc_fileField.gridy = 0;
		this.panel_5.add(fileField, gbc_fileField);
		fileField.setColumns(10);
		
		this.openButton = new JButton("Open");
		this.openButton.setFocusable(false);
		this.openButton.setMargin(new Insets(5, 5, 5, 5));
		this.openButton.addActionListener(new ActionListener() {
			final public void actionPerformed(ActionEvent arg0) {
				JFileChooser fileOpen = new JFileChooser();
				FileFilter filter = new FileNameExtensionFilter("Audio Files (*.mp3)", "mp3");
				fileOpen.setAcceptAllFileFilterUsed(false);
				fileOpen.addChoosableFileFilter(filter);
				int dialog = fileOpen.showOpenDialog(content);
				if (dialog == JFileChooser.APPROVE_OPTION) {
				    File file = fileOpen.getSelectedFile();;
				    fileField.setText(file.getName());
				}
			}
		});
		GridBagConstraints gbc_openButton = new GridBagConstraints();
		gbc_openButton.anchor = GridBagConstraints.EAST;
		gbc_openButton.insets = new Insets(5, 5, 5, 5);
		gbc_openButton.gridx = 1;
		gbc_openButton.gridy = 0;
		this.panel_5.add(this.openButton, gbc_openButton);
		
		this.SIPanel = new JPanel();
		this.SIPanel.setBorder(new TitledBorder(null, "Song Information", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_SIPanel = new GridBagConstraints();
		gbc_SIPanel.insets = new Insets(0, 0, 5, 0);
		gbc_SIPanel.fill = GridBagConstraints.BOTH;
		gbc_SIPanel.gridx = 0;
		gbc_SIPanel.gridy = 1;
		content.add(this.SIPanel, gbc_SIPanel);
		GridBagLayout gbl_SIPanel = new GridBagLayout();
		gbl_SIPanel.columnWidths = new int[]{2147483647, 0};
		gbl_SIPanel.rowHeights = new int[]{0, 2147483647, 0};
		gbl_SIPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_SIPanel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		this.SIPanel.setLayout(gbl_SIPanel);
		
		this.panel_4 = new JPanel();
		GridBagConstraints gbc_panel_4 = new GridBagConstraints();
		gbc_panel_4.anchor = GridBagConstraints.WEST;
		gbc_panel_4.insets = new Insets(0, 0, 5, 0);
		gbc_panel_4.gridx = 0;
		gbc_panel_4.gridy = 0;
		this.SIPanel.add(this.panel_4, gbc_panel_4);
		GridBagLayout gbl_panel_4 = new GridBagLayout();
		gbl_panel_4.columnWidths = new int[]{0, 0, 0};
		gbl_panel_4.rowHeights = new int[]{0, 0};
		gbl_panel_4.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		gbl_panel_4.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		this.panel_4.setLayout(gbl_panel_4);
		
		this.addRowButton = new JButton("Add Row");
		this.addRowButton.setFocusable(false);
		this.addRowButton.setMargin(new Insets(5, 5, 5, 5));
		this.addRowButton.addActionListener(new ActionListener() {
			final public void actionPerformed(ActionEvent arg0) {
				int count = rowData.size() + 1;
				String newCount;
				if (count < 10) {
					newCount = "0" + String.valueOf(count); 
				} else {
					newCount = String.valueOf(count);
				}
				Vector<Object> vec = new Vector<Object>();
				vec.add(newCount);
				vec.add("");
				vec.add("");
				vec.add("");
				GUI.getRowData().add(vec);
				GUI.getTable().fireTableDataChanged();
			}
		});
		GridBagConstraints gbc_addRowButton = new GridBagConstraints();
		gbc_addRowButton.anchor = GridBagConstraints.WEST;
		gbc_addRowButton.insets = new Insets(5, 5, 5, 5);
		gbc_addRowButton.gridx = 0;
		gbc_addRowButton.gridy = 0;
		this.panel_4.add(this.addRowButton, gbc_addRowButton);
		
		this.deleteRowButton = new JButton("Delete Row");
		this.deleteRowButton.setFocusable(false);
		this.deleteRowButton.addActionListener(new ActionListener() {
			final public void actionPerformed(ActionEvent arg0) {
				int count = rowData.size();
				if (count > 1) {
					((DefaultTableModel) table.getModel()).removeRow(count - 1);
				} else {
					table.getModel().setValueAt("", 0, 1);
					table.getModel().setValueAt("", 0, 2);
					table.getModel().setValueAt("", 0, 3);
				}
			}
		});
		this.deleteRowButton.setMargin(new Insets(5, 5, 5, 5));
		GridBagConstraints gbc_deleteRowButton = new GridBagConstraints();
		gbc_deleteRowButton.anchor = GridBagConstraints.WEST;
		gbc_addRowButton.insets = new Insets(5, 5, 5, 5);
		gbc_deleteRowButton.gridx = 1;
		gbc_deleteRowButton.gridy = 0;
		this.panel_4.add(this.deleteRowButton, gbc_deleteRowButton);
		
		this.scrollPane = new JScrollPane();
		this.scrollPane.setAutoscrolls(true);
		this.scrollPane.setViewportBorder(null);
		this.scrollPane.setBorder(null);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.insets = new Insets(5, 5, 5, 5);
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		this.SIPanel.add(this.scrollPane, gbc_scrollPane);
		
		table = new JTable(rowData, this.columnNames);
		table.setRowSelectionAllowed(false);
		table.setAutoCreateRowSorter(true);
		table.setColumnSelectionAllowed(true);
		table.setCellSelectionEnabled(true);
		table.setFillsViewportHeight(true);
		this.scrollPane.setViewportView(table);
		
		this.ButtonsPanel = new JPanel();
		GridBagConstraints gbc_ButtonsPanel = new GridBagConstraints();
		gbc_ButtonsPanel.fill = GridBagConstraints.BOTH;
		gbc_ButtonsPanel.gridx = 0;
		gbc_ButtonsPanel.gridy = 2;
		content.add(this.ButtonsPanel, gbc_ButtonsPanel);
		GridBagLayout gbl_ButtonsPanel = new GridBagLayout();
		gbl_ButtonsPanel.columnWidths = new int[]{116, 0, 0};
		gbl_ButtonsPanel.rowHeights = new int[]{0, 0};
		gbl_ButtonsPanel.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		gbl_ButtonsPanel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		this.ButtonsPanel.setLayout(gbl_ButtonsPanel);
		
		this.saveButton = new JButton("Save");
		this.saveButton.setFocusable(false);
		this.saveButton.setMargin(new Insets(5, 5, 5, 5));
		this.saveButton.addActionListener(new ActionListener() {
			final public void actionPerformed(ActionEvent arg0) {
				CueFileManager.saveCueFile();
			}
		});
		GridBagConstraints gbc_saveButton = new GridBagConstraints();
		gbc_saveButton.insets = new Insets(0, 2, 3, 0);
		gbc_saveButton.anchor = GridBagConstraints.WEST;
		gbc_saveButton.gridx = 0;
		gbc_saveButton.gridy = 0;
		this.ButtonsPanel.add(this.saveButton, gbc_saveButton);
		
		this.loadButton = new JButton("Load");
		this.loadButton.addActionListener(new ActionListener() {
			final public void actionPerformed(ActionEvent arg0) {
				CueFileManager.loadCueFile();
			}
		});
		this.loadButton.setFocusable(false);
		this.loadButton.setMargin(new Insets(5, 5, 5, 5));
		GridBagConstraints gbc_loadButton = new GridBagConstraints();
		gbc_loadButton.anchor = GridBagConstraints.EAST;
		gbc_loadButton.insets = new Insets(0, 3, 3, 0);
		gbc_loadButton.gridx = 1;
		gbc_loadButton.gridy = 0;
		this.ButtonsPanel.add(this.loadButton, gbc_loadButton);
	}
	
	public static JPanel getContent() {
		return content;
	}

	public static JTextField getArtistField() {
		return artistField;
	}

	public static JTextField getTitleField() {
		return titleField;
	}

	public static JTextField getFileField() {
		return fileField;
	}

	public static Vector<Object> getRowData() {
		return rowData;
	}
	
	public static DefaultTableModel getTable() {
		return (DefaultTableModel) table.getModel();
	}
}
